package com.example.springmvcadv.controller;

import com.example.springmvcadv.ex.MyException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/test")
public class TestController {

    private static final Map<String, Object> map = new HashMap<>();

    @RequestMapping("/1")
    public Object t1(){
        return map;
    }

    @RequestMapping("/2")
    public Object t2(String username) throws InterruptedException {

        //模拟用户登录
        //如果没有登录，返回错误信息：可以自定义异常+异常统一处理
        if("abc".equals(username))
            throw new MyException("用户名或密码错误");

        //模拟程序bug
        else if("cba".equals(username)) {
            Object o = null;
                    //NullPointerException
            o.wait();
        }
        return map;
    }

    @RequestMapping("/3")
    public Object t3(){
        Map<String, Object> map = new HashMap<>();
        map.put("username", "猴哥");
        map.put("age", 500);
        return map;
    }
}
